home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Macintosh Tracker 1.20 / source / Server⁄Trecker 2.01 / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-23  |  5.7 KB  |  279 lines  |  [TEXT/KAHL]

  1. /* Main.c */
  2.  
  3. /* this file is based on "Simple Trecker.c" by Frank Seide */
  4.  
  5.  
  6. #include <sound.h>
  7. #include <GestaltEqu.h>
  8. #include <Power.h>
  9. #include "PSyn.h"
  10. #include "STrI.h"
  11. #include "mac_event.h"
  12.  
  13. #define CREATORCODE ('∫Tsq')
  14. #define WAITNEXTEVENTDELAY (30)
  15.  
  16.  
  17. extern Boolean                                ReceivedOpenEventFlag;
  18. extern char                                        FakeKeyBuffer[MAXKEYS];
  19. extern int                                        KeyBufPtr;
  20.  
  21. extern Boolean                                QuitPending;
  22.  
  23. /* parameters controlling the synthesis, with handy default values. */
  24. extern short                                    AntiAliasing;
  25. extern short                                    StereoOn;
  26. extern unsigned short                    SamplingRate;
  27. extern short                                    NumRepeats;
  28. extern short                                    Speed;
  29. extern short                                    StereoMix;
  30. extern short                                    Loudness;
  31.  
  32. extern int                                        RecalibratePlayer; /* set when settings change */
  33.  
  34. extern FSSpec                                    GlobalFileSpec;
  35.  
  36.  
  37. static long                                        PowerManagerInfo = 0;
  38.  
  39. static PChannelPtr                        pc = NULL;
  40. static SoundTrackHandle                strk = NULL;
  41.  
  42. static Boolean                                Pausing = false;
  43.  
  44.  
  45.  
  46. void                SetParameters(void);
  47.  
  48.  
  49. void                discard_buffer(void)
  50.     {
  51.         StopPChannel(pc, true);        /* stop playing with fadeout */
  52.         UnlinkSoundTrack(strk);        /* Unlink STrk from channel */
  53.     }
  54.  
  55.  
  56. void                HandleKeyBuffer(void)
  57.     {
  58.         WaitForEvent(15);
  59.      LoopPoint:
  60.         if (KeyBufPtr > 0)
  61.             {
  62.                 int                    Scan;
  63.                 int                    KeyTemp;
  64.  
  65.                 KeyTemp = (unsigned char)FakeKeyBuffer[0];
  66.                 for (Scan = 1; Scan < KeyBufPtr; Scan += 1)
  67.                     {
  68.                         FakeKeyBuffer[Scan - 1] = FakeKeyBuffer[Scan];
  69.                     }
  70.                 KeyBufPtr -= 1;
  71.                 switch (KeyTemp)
  72.                     {
  73.                         case '+':
  74.                 Loudness += 8;
  75.                 if (Loudness > 64)
  76.                   {
  77.                     Loudness = 64;
  78.                   }
  79.                             SetParameters();
  80.                             UpdateSoundTrack(strk);
  81.                             break;
  82.                         case '-':
  83.                 Loudness -= 8;
  84.                 if (Loudness < 0)
  85.                   {
  86.                     Loudness = 0;
  87.                   }
  88.                             SetParameters();
  89.                             UpdateSoundTrack(strk);
  90.                             break;
  91.                         case ' ':
  92.                             TogglePause();
  93.                             break;
  94.                         case '>':
  95.                             /* (**strk).musicRecord.fastForward = 3; */
  96.                             UpdateSoundTrack(strk);
  97.                             break;
  98.                         case '|':
  99.                             /* (**strk).musicRecord.fastForward = 1; */
  100.                             UpdateSoundTrack(strk);
  101.                             break;
  102.                         case '<':
  103.                             StopPChannel(pc,false);
  104.                             ResetPChannel(pc);
  105.                             StartPChannel(pc);
  106.                             break;
  107.                     }
  108.                 goto LoopPoint;
  109.             }
  110.     }
  111.  
  112.  
  113. void            SetParameters(void)
  114.     {
  115.         int            i;
  116.  
  117.         pc->antiAlias = AntiAliasing;
  118.         pc->userMixing = false;
  119.         pc->noSurround = StereoOn;
  120.         StereoPChannel(pc,(Boolean)StereoOn);
  121.  
  122.         GetSoundTrackWorkspace(strk)->loopDetect = (NumRepeats != 0);
  123.  
  124. #if 0
  125.         for (i = 0; i < 32; i++)
  126.             {
  127.                 PChannelVolume (pc, i, 0x10000);    /* Fixed 0x10000 = 100% */
  128.             }
  129. #endif
  130.         PChannelVolume (pc, -1, (0x10000 * (long)Loudness) / 64);    /* -1 = Gesamtlautst. / -1 denotes total volume */
  131.     }
  132.  
  133.  
  134. void            main(void)
  135.     {
  136.         short                    i;
  137.         short                    WorkingDirectoryRefNum;
  138.         OSErr                    Error;
  139.         long                    ProcTypeInfo;
  140.  
  141.         /* this may not be necessary, but I'll do it anyway to avoid problems */
  142.         InitGraf (&thePort);
  143.         InitFonts();
  144.         FlushEvents (everyEvent, 0);
  145.         InitWindows();
  146.         InitMenus();
  147.         TEInit();
  148.         InitDialogs (NULL);
  149.          InitCursor();
  150.  
  151.         if (!RegisterEventHandlers())
  152.             {
  153.                 return;
  154.             }
  155.         Error = Gestalt(gestaltPowerMgrAttr,&PowerManagerInfo);
  156.         if (Error != noErr)
  157.             {
  158.                 return;
  159.             }
  160. #if 0
  161.         /* 68020 is no longer required */
  162.         Error = Gestalt(gestaltProcessorType,&ProcTypeInfo);
  163.         if ((Error != noErr) || (ProcTypeInfo == gestalt68000))
  164.             {
  165.                 FatalError(FatalError68020NeededID);
  166.                 return;
  167.             }
  168. #endif
  169.  
  170.         if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
  171.             {
  172.                 DisableIdle();
  173.             }
  174.  
  175. #ifdef AskForFile
  176.         {
  177.             Point                                Thing = {50,50};
  178.             StandardFileReply        R;
  179.  
  180.             /* this is only used for debugging when we don't have access to the interface */
  181.             /* program (since the interface program can't link to this program when it */
  182.             /* is being run under the debugger.) */
  183.             StandardGetFile(NULL,-1,NULL,&R);
  184.             GlobalFileSpec = R.sfFile;
  185.         }
  186. #else
  187.         /* waiting for open document command to come */
  188.         while (!ReceivedOpenEventFlag && !QuitPending)
  189.             {
  190.                 WaitForEvent(60);
  191.             }
  192.         if (QuitPending)
  193.             {
  194.                 goto ExitPoint;
  195.             }
  196. #endif
  197.  
  198.         if (OpenPChannel (32,(StereoOn ? opc_stereo : 0) + opc_16Bit,820*8,&pc))
  199.             {
  200.                 FatalError(FatalErrorInternalError);
  201.                 goto ExitPoint;
  202.             }
  203.  
  204.         /* change to desired frequency */
  205.         pc->hardFreq = (unsigned long)SamplingRate << 16;
  206.  
  207.         /* set maximum volume for 4 voices. */
  208.         for (i = 0; i < 4; i += 1)
  209.             {
  210.                 PChannelVolume(pc,i,0x10000);
  211.             }
  212.  
  213.         /* now loading the song.  First, coerce the FSSpec into a working directory */
  214.         OpenWD(GlobalFileSpec.vRefNum,GlobalFileSpec.parID,
  215.             CREATORCODE,&WorkingDirectoryRefNum);
  216.         if (GetSoundTrack(WorkingDirectoryRefNum,GlobalFileSpec.name,0,&strk,FALSE))
  217.             {
  218.                 CloseWD(WorkingDirectoryRefNum);
  219.                 FatalError(FatalErrorNotASong);
  220.                 goto ExitPoint;
  221.             }
  222.         CloseWD(WorkingDirectoryRefNum);
  223.  
  224.         LinkSoundTrack (strk, pc);
  225.  
  226.         SetParameters();
  227.  
  228.         ResetPChannel (pc);
  229.         if (StartPChannel(pc))
  230.             {
  231.                 FatalError(FatalErrorInternalError);
  232.                 goto PreExitPoint;
  233.             }
  234.  
  235.         /* now we do event loop waiting for codes & finish... */
  236.         while (!QuitPending)
  237.             {
  238.                 if (RecalibratePlayer)
  239.                     {
  240.                         SetParameters();
  241.                         UpdateSoundTrack(strk);
  242.                     }
  243.                 HandleKeyBuffer(); /* calls WaitForEvent */
  244.                 /* is the soundtrack done? */
  245.                 if (GetSoundTrackWorkspace(strk)->nextOne)
  246.                     {
  247.                         QuitPending = true;
  248.                     }
  249.             }
  250.  
  251.      PreExitPoint:
  252.         ClosePChannel(pc);
  253.  
  254.      ExitPoint:
  255.         if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
  256.             {
  257.                 EnableIdle();
  258.             }
  259.     }
  260.  
  261.  
  262. void                TogglePause(void)
  263.     {
  264.         if (Pausing)
  265.             {
  266.                 Pausing = false;
  267.                 StartPChannel(pc);
  268.             }
  269.          else
  270.             {
  271.                 Pausing = true;
  272.                 StopPChannel(pc,false);
  273.                 while (Pausing && !QuitPending)
  274.                     {
  275.                         HandleKeyBuffer();
  276.                     }
  277.             }
  278.     }
  279.